package com.example.hospitalmanagement.activity

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.adapter.DeskDoctorDateAdapter
import com.example.hospitalmanagement.bean.DeskDateItem
import com.example.hospitalmanagement.fragment.DoctorScheduleFragment
import com.example.hospitalmanagement.fragment.NoMoreInformationFragment
import kotlinx.android.synthetic.main.activity_desk_doctor.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 医生值班情况Activity
 *
 */
class DeskDoctorActivity : AppCompatActivity() {
    private var startDate=0
    private var endDate=6
    private var state=1
    private var startDateOfWeek=0
    private val fragments:List<Fragment> = listOf(NoMoreInformationFragment(),DoctorScheduleFragment())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_desk_doctor)
        initView()
    }

    @SuppressLint("SimpleDateFormat")
    private fun initView() {
        val fragmentManager=supportFragmentManager.beginTransaction()
        fragments.forEach {
            fragmentManager.add(R.id.desk_doctor_frame,it).hide(it)
        }
        fragmentManager.show(fragments[0])
        fragmentManager.commit()
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation=LinearLayoutManager.HORIZONTAL
        desk_date_item.layoutManager=linearLayoutManager
        val dataList:MutableList<DeskDateItem> = mutableListOf()
        for (index in 0..6){
            startDateOfWeek=index
            dataList.add(
                DeskDateItem(
                when(getAnyDateOfWeek(startDateOfWeek)){
                    1->"日"
                    2->"一"
                    3->"二"
                    4->"三"
                    5->"四"
                    6->"五"
                    7->"六"
                    else->"-1"
                },
                    when{
                        getAnyData(startDateOfWeek).endsWith("01")-> "1"
                        getAnyData(startDateOfWeek).endsWith("02")-> "2"
                        getAnyData(startDateOfWeek).endsWith("03")-> "3"
                        getAnyData(startDateOfWeek).endsWith("04")-> "4"
                        getAnyData(startDateOfWeek).endsWith("05")-> "5"
                        getAnyData(startDateOfWeek).endsWith("06")-> "6"
                        getAnyData(startDateOfWeek).endsWith("07")-> "8"
                        getAnyData(startDateOfWeek).endsWith("09")-> "9"
                        else->getAnyData(startDateOfWeek).substring((getAnyData(startDateOfWeek).length-2),(getAnyData(startDateOfWeek).length))
                    },
                index==0,
                    getAnyData(startDateOfWeek),
                    index==3
            )
            )
        }
        Log.e("DeskDoctorActivity","${dataList.size}")
        val adapter=DeskDoctorDateAdapter(this)
        adapter.dataList=dataList
        desk_date_item.adapter=adapter
        adapter.setListener {
            dataList.forEachIndexed{index, deskDateItem ->
                if (index != it) {
                    false.also { deskDateItem.checked = it }
                } else {
                    deskDateItem.checked = true
                }
            }
            val intent=intent
            val deskId = intent.getIntExtra("deskId", -1)
            val allDate = dataList[it].allDate
            adapter.notifyDataSetChanged()
        }

        desk_start_date_text.text=SimpleDateFormat("yyyy-MM-dd").format(Date())
        desk_end_date_text.text=getAnyData(endDate)
        desk_end_date.setColorFilter(getColor(R.color.dialog_button_color))
        desk_end_date.setOnClickListener {
            when (state){
                in 1..8->{
                    state+=1
                    startDate += 7
                    endDate += 7
                    desk_start_date.setColorFilter(getColor(R.color.dialog_button_color))
                    desk_end_date.setColorFilter(getColor(R.color.dialog_button_color))
                    desk_start_date_text.text = getAnyData(startDate)
                    desk_end_date_text.text = getAnyData(endDate)
                }
                9->{
                    state+=1
                    startDate += 7
                    endDate += 7
                    desk_start_date.setColorFilter(getColor(R.color.dialog_button_color))
                    desk_end_date.setColorFilter(getColor(R.color.text_color))
                    desk_start_date_text.text = getAnyData(startDate)
                    desk_end_date_text.text = getAnyData(endDate)
                }
                else->{ }
            }
            if (startDateOfWeek<63) {
                dataList.clear()
                for (index in 1..7) {
                    dataList.add(
                        DeskDateItem(
                            when(getAnyDateOfWeek((startDateOfWeek+index))){
                                1->"日"
                                2->"一"
                                3->"二"
                                4->"三"
                                5->"四"
                                6->"五"
                                7->"六"
                                else->"-1"
                            },
                            when{
                                getAnyData((startDateOfWeek+index)).endsWith("01")-> "1"
                                getAnyData((startDateOfWeek+index)).endsWith("02")-> "2"
                                getAnyData((startDateOfWeek+index)).endsWith("03")-> "3"
                                getAnyData((startDateOfWeek+index)).endsWith("04")-> "4"
                                getAnyData((startDateOfWeek+index)).endsWith("05")-> "5"
                                getAnyData((startDateOfWeek+index)).endsWith("06")-> "6"
                                getAnyData((startDateOfWeek+index)).endsWith("07")-> "8"
                                getAnyData((startDateOfWeek+index)).endsWith("09")-> "9"
                                else->getAnyData((startDateOfWeek+index)).substring((getAnyData((startDateOfWeek+index)).length-2),(getAnyData((startDateOfWeek+index)).length))
                            },
                            false,
                            getAnyData((startDateOfWeek+index)),
                            false
                        )
                    )
                }
                startDateOfWeek+=7
                adapter.notifyDataSetChanged()
            }
        }
        desk_start_date.setColorFilter(getColor(R.color.text_color))
        desk_start_date.setOnClickListener {
            when(state) {
                2 -> {
                    state -= 1
                    startDate -= 7
                    endDate -= 7
                    desk_start_date.setColorFilter(getColor(R.color.text_color))
                    desk_end_date.setColorFilter(getColor(R.color.dialog_button_color))
                    desk_start_date_text.text = getAnyData(startDate)
                    desk_end_date_text.text = getAnyData(endDate)
                }
                in 3..10 -> {
                    state -= 1
                    startDate -= 7
                    endDate -= 7
                    desk_start_date.setColorFilter(getColor(R.color.dialog_button_color))
                    desk_end_date.setColorFilter(getColor(R.color.dialog_button_color))
                    desk_start_date_text.text = getAnyData(startDate)
                    desk_end_date_text.text = getAnyData(endDate)
                }
                else -> { }
            }
            if (startDateOfWeek>7) {
                dataList.clear()
                startDateOfWeek-=7
                for (index in 6 downTo 0) {
                    dataList.add(
                        DeskDateItem(
                            when(getAnyDateOfWeek((startDateOfWeek-index))){
                                1->"日"
                                2->"一"
                                3->"二"
                                4->"三"
                                5->"四"
                                6->"五"
                                7->"六"
                                else->"-1"
                            },
                            when{
                                getAnyData((startDateOfWeek-index)).endsWith("01")-> "1"
                                getAnyData((startDateOfWeek-index)).endsWith("02")-> "2"
                                getAnyData((startDateOfWeek-index)).endsWith("03")-> "3"
                                getAnyData((startDateOfWeek-index)).endsWith("04")-> "4"
                                getAnyData((startDateOfWeek-index)).endsWith("05")-> "5"
                                getAnyData((startDateOfWeek-index)).endsWith("06")-> "6"
                                getAnyData((startDateOfWeek-index)).endsWith("07")-> "8"
                                getAnyData((startDateOfWeek-index)).endsWith("09")-> "9"
                                else->getAnyData((startDateOfWeek-index)).substring((getAnyData((startDateOfWeek-index)).length-2),(getAnyData((startDateOfWeek-index)).length))
                            },
                            when((startDateOfWeek-index)){0->true else->false},
                            getAnyData((startDateOfWeek-index)),
                            false
                        )
                    )
                }
                adapter.notifyDataSetChanged()
            }
        }
    }

    /**
     * 获取当前时间开始任意天数后的时间
     */
    private fun getAnyData(addData:Int):String{
       val rightNow=Calendar.getInstance()
        rightNow.add(Calendar.DAY_OF_YEAR,addData)
        val sdf=SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        val date=rightNow.time
        return sdf.format(date)
    }
    /**
     * 获取当前时间开始任意天数后的时间所在的星期数
     */
    private fun getAnyDateOfWeek(addData: Int):Int{
        val rightNow=Calendar.getInstance()
        rightNow.add(Calendar.DAY_OF_YEAR,addData)
        return rightNow.get(Calendar.DAY_OF_WEEK)
    }
}